'use strict'

const { ApiController } = require('jax')

class Controller extends ApiController {
  get Model() {
    return this.ctx.model.Dict
  }

  // 查询前
  async beforeIndex() {
    const { ctx } = this
    const { query } = ctx
    const { helper } = ctx
    const filter = {}

    // 关键字模糊查询
    if (query.keyword && !query.keyword == '') {
      const reg = new RegExp(helper.toMongoRegex(query.keyword, 'i'))
      filter.$or = [{ name: reg }, { type: reg }, { remark: reg }]
    }

    return {
      filter,
    }
  }

  // 保存后
  async afterSave() {
    this.service.dict.updateDict() // 更新字典
  }

  // 删除后
  async afterDestroy() {
    this.service.dict.updateDict() // 更新字典
  }
}

module.exports = Controller
